package com.lingchen.service.impl;

import com.lingchen.dao.CourseDao;
import com.lingchen.dao.UserDao;
import com.lingchen.entity.Collect;
import com.lingchen.entity.Course;
import com.lingchen.entity.User;
import com.lingchen.enums.MenuEnum;
import com.lingchen.nto.Menu;
import com.lingchen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Author    lingchen
 * Email     838878458@qq.com
 * Time      2018/2/26
 * Function  用户中心
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private CourseDao courseDao;

    public List<Menu> getMenu() {
        //这里拼接菜单
        List<Menu> menus = new ArrayList<Menu>();
        //查询所有科目的考试
        List<Course> courses = courseDao.selectCourse();
        //考试与收藏 不需要科目二与三
        List<Course> collect = new ArrayList<Course>(2);
        collect.add(courses.get(0));
        collect.add(courses.get(3));
        //生成考试菜单
        Menu examMenu = new Menu(MenuEnum.MENU_EXAM, collect);

        //生成练习菜单
        Menu exerciseMenu = new Menu(MenuEnum.MENU_EXERCISE, courses);
        //生成收藏菜单
        Menu collectMenu = new Menu(MenuEnum.MENU_COLLECT, collect);
        //错题集
        Menu errorMenu = new Menu(MenuEnum.MENU_ERROR, collect);
        menus.add(examMenu);
        menus.add(exerciseMenu);
        menus.add(collectMenu);
        menus.add(errorMenu);
        return menus;
    }

    public int insertUser(User user) {
        //如果已经包含用户 直接登录
        if (selectUser(user) != null)
            return 1;
        return userDao.insertUser(user);
    }

    public User selectUser(User user) {
        return userDao.selectUser(user);
    }

    public int insertCollects(Collect... collects) {
        return userDao.insertCollects(collects);
    }

    public List<Collect> selectCollects(long userName) {
        return userDao.insertCollects(userName);
    }
}
